/**
 * 
 */
package edu.pku.sei.modeler.editparts;

import java.beans.PropertyChangeEvent;

import org.eclipse.draw2d.ChopboxAnchor;
import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.DirectEditPolicy;
import org.eclipse.gef.requests.DirectEditRequest;

import edu.pku.sei.modeler.figures.PortFigure;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.modeler.model.structure.StructurePackage;
import edu.pku.sei.modeler.model.uml.Ports.Port;
import edu.pku.sei.modeler.uml.commands.ChangeNameCommand;
import edu.pku.sei.modeler.uml.modelHelper.ModelPackage;

/**
 * @author YanYan
 * @author zsj
 * @author fengchao
 */
public class PortEditpart extends AdsorbNodeEditpart {

	public PortEditpart(MetaShapeElement model) {
		super(model);
		offsetX = -(PortFigure.WIDTH/2);
		offsetY = -(PortFigure.HEIGHT/2);
	}

	public Port getModelElement() {
		MetaNode node = (MetaNode) getModel();
		return (Port) node.getModelElement();
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.pku.sei.modeler.edit.parts.BaseEditPart#createDirectEditPolicy()
	 */
	@Override
	protected DirectEditPolicy createDirectEditPolicy() {
		return new DirectEditPolicy() {
			protected Command getDirectEditCommand(DirectEditRequest request) {
				return new ChangeNameCommand(getModelElement(),
						(String) request.getCellEditor().getValue());
			}

			protected void showCurrentEditValue(DirectEditRequest request) {
				IFigure fig = getDirectEditFigure();
				if (fig instanceof Label) {
					((Label) fig).setText(getModelElement().getName());
					fig.getUpdateManager().performUpdate();
				}
			}
		};
	}
	@Override
	protected IFigure getDirectEditFigure() {
		return ((PortFigure)getFigure()).getNameLabel();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
	 */
	@Override
	protected IFigure createFigure() {
		PortFigure figure = new PortFigure();
		figure.setDisplayString(getModelElement().getName());
		return figure;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
	 */
	protected void createEditPolicies() {
		super.createEditPolicies();
	}

	protected void handlePropertyChanged(PropertyChangeEvent event) {
		String propertyName = event.getPropertyName();
		if (ModelPackage.NAMEDELEMENT_NAME.equals(propertyName)) {
			((PortFigure)getFigure()).setDisplayString((String) event.getNewValue());
		} else {
			super.handlePropertyChanged(event);
		}
	}

	protected void refreshVisuals() {
		Rectangle constraint = new Rectangle(0, 0, -1, -1);
		MetaNode node = getNode();
		Point p;
		if (adsorbed) {
			p = calculateLocation(node.getLocationX(), node.getLocationY());
			node.setLocationX(p.x);
			node.setLocationY(p.y);
		} else {
			p = new Point(node.getLocationX(), node.getLocationY());
		}
		constraint.setLocation(p);
		((GraphicalEditPart) getParent()).setLayoutConstraint(this,
				getFigure(), constraint);
	}

	public ConnectionAnchor getSourceConnectionAnchor(
			ConnectionEditPart connection) {
		if (anchor == null)
			anchor = new ChopboxAnchor(((PortFigure)getFigure()).getRectangle());
		return anchor;
	}

	public ConnectionAnchor getSourceConnectionAnchor(Request request) {
		if (anchor == null)
			anchor = new ChopboxAnchor(((PortFigure)getFigure()).getRectangle());
		return anchor;
	}

	public ConnectionAnchor getTargetConnectionAnchor(
			ConnectionEditPart connection) {
		if (anchor == null)
			anchor = new ChopboxAnchor(((PortFigure)getFigure()).getRectangle());
		return anchor;
	}

	public ConnectionAnchor getTargetConnectionAnchor(Request request) {
		if (anchor == null)
			anchor = new ChopboxAnchor(((PortFigure)getFigure()).getRectangle());
		return anchor;
	}

}
